﻿using Agile.Core;
using Agile.Core.Environments;
using Agile.Core.Globals;
using Agile.Core.IPlugins;
using Agile.Entity;
using DevExpress.XtraTab;
using Service.Inside;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

//开发测试框架
namespace Agile.Develop
{
    public partial class ParentForm : DevExpress.XtraEditors.XtraForm
    {
        private string _UserCode = "test";//用户名
        private string _PassWord = "123";//密码

        #region 初始化
        public ParentForm()
        {
            InitializeComponent();
            this.Initialize();
        }
        private void Initialize()//初始化
        {
            tree_Plguin.DataSource = this.LoadPluginList();
            tree_Plguin.CollapseAll();
            tree_Plguin.ExpandAll();
            AgileDebug.Message(DebugType.调试输出, "系统初始化", "正在读取程序配置...");
            Agile.Core.Common.InitEnvironment();
            AgileDebug.Message(DebugType.调试输出, "系统初始化", string.Format("操作系统[{0}]，NetFrameWork[{1}]，计算机名[{2}]，IP[{3}]，MAC[{4}]", Computer.Windows, Computer.FrameWork, Computer.HostName, Computer.IPaddress, Computer.NetworkID));
            AgileDebug.Message(DebugType.调试输出, "连接服务器", SysOptions.Pattern == Pattern.Localhost ? Computer.HostName : string.Format("{0}:{1}", Connection.Server.Address, Connection.Server.Port));
            var _Service = ServiceBridge.CreateService<IInside>(typeof(InsideService));
            AgileDebug.Message(DebugType.调试输出, "用户验证", string.Format("用户名[{0}]，密码[{1}]", _UserCode, _PassWord));
            Agile.Core.Common.LoginSystem(_UserCode, _PassWord);
            Agile.Core.Common.InitLicense();
            Agile.Core.Common.InitSystem(this.TabControl, false);
            this.barItemUser.Caption = string.Format("{0}({1})", User.Name, User.Code);
            this.barItemDept.Caption = User.DeptName;
            this.barItemModel.Caption = string.Format("{0}[{1}]", Register.Caption, Register.xType);
            Sys_Record record = _Service.GetUserRecord(User.UID);
            AgileDebug.Message(DebugType.用户操作, "用户登录", string.Format("用户[{0}]登录系统成功.", User.Code));
            AlertControlEx.Show(this, string.Format("欢迎, {0}({1})", User.Name, User.Code), string.Format("上次登陆: {0}\r\n网络地址: {1}\r\n计算机名: {2}", record.ChangeDate, record.Address, record.HostName));
            record.ChangeDate = DateTime.Now;
            _Service.UpdateUserRecord(record);//保存用户登录记录
            Agile.Core.WaitFormEx.Close();
        }
        private List<PluginEntity> LoadPluginList()//载入配置文件组件集
        {
            List<PluginEntity> plugins = new List<PluginEntity>();
            //plugins.Add(new PluginEntity { ID = "组件", ParentID = "Root", Caption = "组件" });
            XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(Path.Combine(Application.StartupPath, "System.config"));
            XmlNodeList nodeList = doc.SelectSingleNode("Root/Plugin").ChildNodes;
            foreach (XmlNode menu in nodeList)
            {
                XmlElement menuElement = (XmlElement)menu;
                PluginEntity menuItem = new PluginEntity();
                menuItem.ID = menuElement.GetAttribute("Caption");
                menuItem.Caption = menuItem.ID;
                menuItem.ParentID = "组件";
                plugins.Add(menuItem);
                XmlNodeList nodes = menu.ChildNodes;
                foreach (XmlNode config in nodes)
                {
                    XmlElement element = (XmlElement)config;
                    PluginEntity configItem = new PluginEntity();
                    configItem.ID = element.GetAttribute("Caption");
                    configItem.FileName = element.GetAttribute("FileName");
                    configItem.FullName = element.GetAttribute("FullName");
                    configItem.SetupPath = element.GetAttribute("SetupPath");
                    configItem.Caption = configItem.ID;
                    configItem.ParentID = menuItem.ID;
                    configItem.IsPlugin = true;
                    plugins.Add(configItem);
                }
            }
            return plugins;
        }
        #endregion

        #region 菜单事件

        private void barStyle_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barPassWord_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barClose_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.TabControl.SelectedTabPage.Dispose();
        }

        private void barCloseAll_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            foreach (XtraTabPage item in this.TabControl.TabPages)
            {
                item.Dispose();
            }
        }

        private void barExit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Application.Exit();
        }

        private void barNotepad_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("notepad.exe");
        }

        private void barCalc_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("calc.exe");
        }

        private void barDebug_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start(Path.Combine(Application.StartupPath, "Debug.exe"));
        }

        private void barBasePath_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", Application.StartupPath);
        }

        private void barPluginsPath_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", Path.Combine(Application.StartupPath, "Plugins"));
        }

        private void barDirvesPath_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", Path.Combine(Application.StartupPath, "Drives"));
        }

        private void barLogPath_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("explorer.exe", Path.Combine(Application.StartupPath, "Log"));
        }
        private void barHelp_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barWebsite_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.baidu.com");
        }

        private void barOpinion_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            System.Diagnostics.Process.Start("http://bbs.baidu.com");
        }

        private void barRegister_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barAbout_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barUser_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.BasicLibrary.dll", "Plugin.BasicLibrary.Employee");
        }

        private void barDept_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.BasicLibrary.dll", "Plugin.BasicLibrary.Department");
        }

        private void barAuthorized_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.Hypervisor.dll", "Plugin.Hypervisor.Authorized");
        }

        private void barComponent_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.Configure.dll", "Plugin.Configure.Script");
        }

        private void barButtonItem32_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.Configure.dll", "Plugin.Configure.Module");
        }

        private void barDictionary_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.Configure.dll", "Plugin.Configure.DictMgmt");
        }

        private void barDescription_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.Configure.dll", "Plugin.Configure.Document");
        }

        private void barQuery_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.Configure.dll", "Plugin.Configure.Analyzer");
        }

        private void barReport_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Agile.Report.dll", "Agile.Report.ReportManage", "");
        }

        private void barButtonItem9_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.Configure.dll", "Plugin.Configure.Sequence");
        }

        private void barCache_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.LoadPluginForm("Plugin.Configure.dll", "Plugin.Configure.Caches");
        }

        private void barLog_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        private void barInit_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {

        }

        #endregion

        private void ParentForm_Load(object sender, EventArgs e)
        {
        }

        private void LoadPluginForm(string fileName, string fullName, string path = "Plugins")
        {
            Agile.Core.WaitFormEx.Show("正在加载,请稍后 ...");
            try
            {
                IPluginForm frm = Agile.Core.ServiceBridge.CreatePluginForm(fileName, fullName, path);
                Agile.Core.WaitFormEx.SetDescription(string.Format("正在加载[{0}], 请稍后...", frm.PluginName));
                frm.ShowForm();
                Agile.Core.WaitFormEx.Close();
            }
            catch (Exception ex)
            {
                Agile.Core.WaitFormEx.Close();
                MessageBoxEx.Show(ex.Message, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void tree_Plguin_DoubleClick(object sender, EventArgs e)//调用组件
        {
            var isPlugin = (bool)tree_Plguin.FocusedNode.GetValue("IsPlugin");
            var fullName = (string)tree_Plguin.FocusedNode.GetValue("FullName");
            var fileName = (string)tree_Plguin.FocusedNode.GetValue("FileName");
            var setupPath = (string)tree_Plguin.FocusedNode.GetValue("SetupPath");
            if (isPlugin == true)
                this.LoadPluginForm(fileName, fullName, setupPath);
        }

        private void navBar_NavPaneMinimizedGroupFormShowing(object sender, DevExpress.XtraNavBar.NavPaneMinimizedGroupFormShowingEventArgs e)
        {
            tree_Plguin.ExpandAll();
        }

        private void tree_Plguin_CustomDrawNodeImages(object sender, DevExpress.XtraTreeList.CustomDrawNodeImagesEventArgs e)
        {
            bool isPlugin = (bool)e.Node.GetValue("IsPlugin");
            if (isPlugin)
                e.SelectImageIndex = 4;
            else if (e.Node.Expanded)
                e.SelectImageIndex = 1;
            else
                e.SelectImageIndex = 0;
        }


    }
}
